package com.lm56.web.network.controller;

import com.lm56.common.MyRequestBody;
import com.lm56.common.MyResponseBody;
import com.lm56.common.exception.FieldValidateExcetiption;
import com.lm56.common.exception.MyException;
import com.lm56.web.network.service.LinesService;
import com.lm56.web.network.domain.CompanyLine;
import com.lm56.web.network.domain.CompanyLineQueryForm;
import com.lm56.web.security.SecUserDetails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by Kelvin Yeung on 2016/11/27.
 */
@Controller
@RequestMapping(value = "/workbench")
public class LinesController {


    Logger logger = LoggerFactory.getLogger(LinesController.class);

    @Autowired
    LinesService linesService;


    /**
     * 线路主页面
     * @return
     */
    @RequestMapping(value = "lines", method = RequestMethod.GET)
    public String lines() {
        return "workbench/company/lines";
    }

    /**
     * 线路列表数据
     * @param requestBody
     * @param result
     * @param model
     * @return
     */
    @RequestMapping(value = "lines", method = RequestMethod.POST)
    @ResponseBody
    public MyResponseBody lines(@RequestBody MyRequestBody<CompanyLineQueryForm> requestBody, BindingResult result, Model model) {

        SecUserDetails secUser = (SecUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        CompanyLineQueryForm params = requestBody.getParams();

        if (params == null) {
            params = new CompanyLineQueryForm();
        }

        params.setUserId(secUser.getUserId());
        requestBody.setParams(params);
        List<CompanyLine> lines = linesService.getCompanyLinesForCompany(requestBody);
        return new MyResponseBody(MyResponseBody.STATUS_SUCESS, "获取线路列表成功", lines);
    }

    /**
     * 新增线路页面
     * @return
     */
    @RequestMapping(value = "line/add", method = RequestMethod.GET)
    public String addLine() {
        return "workbench/company/line_add" ;
    }

    /**
     * 新增线路处理
     * @param companyLine
     * @param result
     * @param model
     * @return
     */
    @RequestMapping(value = "line/add", method = RequestMethod.POST)
    public String addLine(@Valid CompanyLine companyLine ,BindingResult result,Model model) {

        if(result.hasErrors()){
            return "workbench/company/line_add";
        }
        try{
            linesService.addLine(companyLine);
        }catch (FieldValidateExcetiption e){
            result.rejectValue(e.getField(),null,e.getMessage());
            return "workbench/company/line_add";
        }

        model.addAttribute("success","success");
        return "workbench/company/line_add" ;
    }


    /**
     * 线路详情页面
     * @return
     */
    @RequestMapping(value = "line/detail",method = RequestMethod.GET)
    public String lineDetail(@RequestParam("id")Long lineId,Model model){
        if(lineId==null){
            throw new MyException("线路不存在");
        }
        SecUserDetails secUser = (SecUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        CompanyLine line = linesService.getCompanyLinesById(secUser.getUserId(),lineId);
        if(line ==null){
            throw new MyException("线路不存在");
        }
        model.addAttribute("companyLine",line);
        return "workbench/company/line_detail";
    }

    @RequestMapping(value="line/detail",method = RequestMethod.POST)
    public String lineDetail(@Valid CompanyLine line,BindingResult result,Model model){


        if(result.hasErrors()){
            return "workbench/company/line_detail";
        }
        try{
            linesService.updateCompanyLine(line);
        }catch (FieldValidateExcetiption e){
            result.rejectValue(e.getField(),null,e.getMessage());
            return "workbench/company/line_detail";
        }

        model.addAttribute("success","success");
        return "workbench/company/line_detail";
    }

    /**
     * 删除线路
     * @param lineId
     * @return
     */
    @RequestMapping(value="line/delete")
    @ResponseBody
    public MyResponseBody deleteLine(@RequestParam("lineId") Long lineId){

        linesService.deleteCompanyLineById(lineId);
        return new MyResponseBody(MyResponseBody.STATUS_SUCESS,"删除成功");
    }



}
